# This Python file uses the following encoding: utf-8
from catchObj.TR import TR
import func
from catchObj.catch import Catch
from MySignals import global_ms
import time
import json
from db import ObjLink
from catchObj.catchTime import CatchTime
from catchObj.Stocks import Stocks
import random


#创建一个线程的时候,需要明确在子类中写明threads,和self.threadId,覆盖掉父类的属性,才能正常运行,,,python有点坑啊
#run 运行完毕,要清空threads,只限于单线程的任务,多线程的任务在stop函数中要清空threads

class ThCatch(TR):

    workNum = 0
    threadsNum = 0
    threads = []
    timeOver = 0

    def __init__(self, threadId):
        super(ThCatch, self).__init__(ThCatch)
        if ThCatch.timeOver == 0:
            print("错误,请先初始化timeOver")
        self.threadId = threadId

    def run(self):
        self.notice_msg("抓取线程开启")
        catch = Catch()
        i = 0
        while self.running.isSet():
            #不在抓取时间段内,退出
            if time.time()>ThCatch.timeOver:
                break

            ThCatch.workNum += 1
            tn = time.time()
            result = catch.catchHtml(TR.getUrls(Stocks.stocksGroups[self.threadId]))
            if result:
                i = i + 1
                catch.saveToRedis()
                tn = time.time()-tn
                self.notice_msg("完成第"+str(i)+"次抓取,耗时:"+str(round(tn,2))+"秒")
                #因为sina是3秒一更新,所以这里尽量吧时间准确美3秒一次,
                if 3>tn:
                    time.sleep(3-tn)
            else:
                self.notice_msg("错误,暂停5秒重新开始")
                time.sleep(5)
        self.notice_msg("抓取线程关闭")

class ThTest(TR):
    threads = []
    def __init__(self,threadId):
        super(ThTest, self).__init__(ThTest)
        self.threadId = threadId
        rt = []
        while True:
            t = random.randint(0,len(Stocks.stocks))
            rt.append(Stocks.stocks[t])
            if len(rt) == 10:
                break
        self.url = TR.getUrls(rt)

    def run(self):
        catch = Catch()
        self.notice_msg("测试抓取中...")
        tf = catch.testOk(self.url)
        self.notice_msg("测试抓取完成")
        global_ms.setTest.emit(tf)
        ThTest.threads = []

class ThToMongo(TR):
    threads = []
    def __init__(self,threadId):
        super(ThToMongo, self).__init__(ThToMongo)
        self.threadId = threadId

    def run(self):
        #开启保存到mongo的同时,也保存当天的数据到日线中,
        self.notice_msg("抓取数据整理到分时数据...")
        self.fsToDaily()
        self.notice_msg("数据整理完成...")
        self.notice_msg("保存到mongo中...")
        self.toMysql()
        self.notice_msg("保存到mongo完成...")
        ThToMongo.threads = []

    #保存实时数据到mongo
    def toMysql(self):
        for st in Stocks.stocks:
            t = []
            list = Stocks.redis.lrange(func.zhuanhuan(st[0]),0,-1)
            for li in list:
                t.append(json.loads(li))
            Stocks.collections.dailis.insert_one({'code':func.zhuanhuan(st[0]),'data':t,'getDate':func.getDate()})


    #更新日线数据
    def fsToDaily(self):
        for st in Stocks.stocks:
            code = st[0]
            t = []
            rfs = Stocks.redis.lrange('fs_'+func.zhuanhuan(code),0,1)
            if len(rfs)>0:
                li = json.loads(rfs[0])
                if len(li)>3:
                    dd = func.cchToDaily(li)
                    doc = Stocks.collections.daily.find_one({'code':code})
                    if doc != None:
                        t = doc['data']
                        t.insert(0,dd)
                        Stocks.collections.daily.update_one(
                            {'code':code},
                            {"$set": {"data":t,'getdate':func.getDate()}},
                            upsert=True 
                        )
                    else:
                        Stocks.collections.daily.insert_one({'code':code,"data":[dd],'getdate':func.getDate()})
                else:
                    print("数据好像出现了问题,code:"+code)
            else:
                print("数据好像出现了问题,没有在redis中找到抓取数据code:"+code)

class ThDelFs(TR):
    threads=[]

    def __init__(self,threadId):
        super(ThDelFs,self).__init__(ThDelFs)        
        self.threadId = threadId

    def run(self):
        self.notice_msg("删除分时数据开始")
        TR.clearMongoData('fs_')
        self.notice_msg("删除分时数据完成")

class ThDelCatch(TR):
    threads=[]

    def __init__(self,threadId):
        super(ThDelCatch,self).__init__(ThDelCatch)
        self.threadId = threadId

    def run(self):
        self.notice_msg("删除抓取数据开始")
        TR.clearMongoData('')
        self.notice_msg("删除抓取数据完成")
